package com.example.jeong.yuejian10.Model;


import com.example.jeong.yuejian10.Bean.ActivityBean;
import com.example.jeong.yuejian10.Bean.ClubBean;
import com.example.jeong.yuejian10.Bean.EnterActBean;
import com.example.jeong.yuejian10.Bean.EnterMatchBean;
import com.example.jeong.yuejian10.Bean.EnterTrainBean;
import com.example.jeong.yuejian10.Bean.MatchBean;
import com.example.jeong.yuejian10.Bean.PostBean;
import com.example.jeong.yuejian10.Bean.ProBean;
import com.example.jeong.yuejian10.Bean.TrainBean;
import com.example.jeong.yuejian10.Bean.UserBean;
import com.example.jeong.yuejian10.Listener.RetrofitListener;
import com.example.jeong.yuejian10.Service.UserService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.jeong.yuejian10.constant.Constant.BASE_URL;


public class UserModel {
    private UserService userService;
    private Retrofit retrofit;
    private int u_id;
    //构造方法（方法名和类名相同）
    public UserModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();


    }
    public void userLogin(String str_phone, String str_upass,final RetrofitListener listener){
        userService = retrofit.create(UserService.class);
        Call call = userService.getUserLogin(str_phone,str_upass);
        Callback<UserBean> callback = new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    public void register(String str_unamer, String str_upass, String str_tel, String str_address,final  RetrofitListener listener){
        userService = retrofit.create(UserService.class);
        Call call = userService.getUserReg(str_unamer,str_upass,str_tel,str_address);
        Callback<UserBean> callback = new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }



    public Call<UserBean> UserInfo(int u_id) {
        UserService userService = retrofit.create(UserService.class);
        return userService.getUserInfo(u_id);
    }


    public Call<UserBean> getUserInfo(int u_id) {
        UserService userService = retrofit.create(UserService.class);
        return userService.getUserInfo(u_id);
    }
    public Call<UserBean> edituser(int u_id) {
        UserService userService = retrofit.create(UserService.class);
        return userService.edituser(u_id);
    }
    public Call<UserBean> updateuser(int u_id ,String u_name,String u_pass,String u_phone) {
        UserService userService = retrofit.create(UserService.class);
        return userService.updateuser(u_id, u_name,u_pass,u_phone);
    }

    public Call<List<PostBean>> getMypostlist(int u_id) {
        UserService userService = retrofit.create(UserService.class);
        return userService.getMypostlist(u_id);
    }


    //查看我的培训订单列表
    public void getMyTrainOrderlist(int u_id,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyTrainOrderlist(u_id);
        Callback<List<EnterTrainBean>> callback = new Callback<List<EnterTrainBean>>() {
            @Override
            public void onResponse(Call<List<EnterTrainBean>> call, Response<List<EnterTrainBean>> response) {
                List<EnterTrainBean> enterTrainBeen = response.body();
                listener.onSuccess(enterTrainBeen);
            }
            @Override
            public void onFailure(Call<List<EnterTrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    //查看我的活动订单列表
    public void getMyActOrderlist(int u_id,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyActOrderlist(u_id);
        Callback<List<EnterActBean>> callback = new Callback<List<EnterActBean>>() {
            @Override
            public void onResponse(Call<List<EnterActBean>> call, Response<List<EnterActBean>> response) {
                List<EnterActBean> enterActBeen = response.body();
                listener.onSuccess(enterActBeen);
            }
            @Override
            public void onFailure(Call<List<EnterActBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //查看我的比赛订单列表
    public void getMyMatchOrderlist(int u_id,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyMatchOrderlist(u_id);
        Callback<List<EnterMatchBean>> callback = new Callback<List<EnterMatchBean>>() {
            @Override
            public void onResponse(Call<List<EnterMatchBean>> call, Response<List<EnterMatchBean>> response) {
                List<EnterMatchBean> enterMatchBean = response.body();
                listener.onSuccess(enterMatchBean);
            }
            @Override
            public void onFailure(Call<List<EnterMatchBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }
    //查看我关注的培训
    public void getMyCollectTrain(int u_id,String flag,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyCollectTrain(u_id,flag);
        Callback<List<TrainBean>> callback = new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                List<TrainBean> trainhBean = response.body();
                listener.onSuccess(trainhBean);
            }
            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }


    public void getMyCollectClub(int u_id,String flag,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyCollectClub(u_id,flag);
        Callback<List<ClubBean>> callback = new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                List<ClubBean> clubBean = response.body();
                listener.onSuccess(clubBean);
            }
            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    //查看我关注的比赛
    public void getMyCollectMatch(int u_id,String flag,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyCollectMatch(u_id,flag);
        Callback<List<MatchBean>> callback = new Callback<List<MatchBean>>() {
            @Override
            public void onResponse(Call<List<MatchBean>> call, Response<List<MatchBean>> response) {
                List<MatchBean> matchBean = response.body();
                listener.onSuccess(matchBean);
            }
            @Override
            public void onFailure(Call<List<MatchBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    public void getMyCollectMaster(int u_id,String flag,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyCollectMaster(u_id,flag);
        Callback<List<ProBean>> callback = new Callback<List<ProBean>>() {
            @Override
            public void onResponse(Call<List<ProBean>> call, Response<List<ProBean>> response) {
                List<ProBean> proBean = response.body();
                listener.onSuccess(proBean);
            }
            @Override
            public void onFailure(Call<List<ProBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    //查看我关注的活动
    public void getMyCollectAct(int u_id,String flag,final RetrofitListener listener){
        UserService userService = retrofit.create(UserService.class);
        Call call = userService.getMyCollectAct(u_id,flag);
        Callback<List<ActivityBean>> callback = new Callback<List<ActivityBean>>() {
            @Override
            public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
                List<ActivityBean> activityBean = response.body();
                listener.onSuccess(activityBean);
            }
            @Override
            public void onFailure(Call<List<ActivityBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

}

